package com.xgzit.xgzmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xgzit.xgzmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117685900";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCifJ2U6eRmQGR4fwVDpn6wrjUl/QvDWiCKMZKZqOt/zFEVL1uTrxzwodbi1aKY8OuHrBYWyUnDPWR6iskcUlIW+xIdL/lvdChg2vXR0dvlSuSPSUhAvMGFnJyMf+ueVtm08r6r1nzdokibDh/uwLN+0hiIBf0F575nIAk2ZRR0f83GMvePZTOC7YighalVh2mnTl0F4uaJL5OTsef1OwArukaMkaWnnrEIhDTSqJjB21ew5s7Ra9wxrq2Ns75fAT0CjVp9LAMIW+VGzIK5KpSxcZXQggjNSgWnqlHUU8gJoLf1bA2rDcJ0VCdksJREeQP2c4YtSQos4uExULjhG+Q1AgMBAAECggEBAI2FjlbaVLV+lkOhFnuysxu9z8rTmIBAqqDSxoYRa3R3B5Q1GEUMO1+XBiuxuRoOlIYpq1+TWCy19yMIaFvDk34ylbdYRp/9gwHBFGP4rdcLIQFv6X+BJ+jw3CU/cYAK0znYR5wxh7Yel7B9X/C6ZFwsL3OiC5wuGVeGvp9i8Kr6FIhLnF1hGNLcmlufNjuhq/gqhaq6Cl5/7s9z1pKwhiBplpGICWb7ovvKSu8AV4gAy51bTAb5C3yaE9cRkkmAJasML2Arrc69tCvskHh+kS7t2VqnKzRUh1PTL4uFZK4Uc7gpDPjE0zBwb+E0QhUcd10Vk82IZvhCwSIRunaLcOECgYEA4o9HjtS+lZ/fxKRPsjwJfwVXZ5wKF9K3M16eHe6bBzgDZ0JmuvcYiSCGMtF+eZgp81cxuDNZL11TMrGM4kpLpwxJTIgIaQiLHIY0XYy400Vr/+D4skYhvEFZFUQ87oNWZpwEECyfci+7Vp6gMEQjGoIiP64kC4/z5gWwfqkYbmkCgYEAt5nkBQNMh09zZ6QacBaU5OyXKI2bIHghHx8TgIRJHXfNtrOExo4TYS9Tu4qrQHPQCw+OIG8BCwfvZJeN2SpYx6mKIy/xucALCdH83mlLcWDnWh1MLuN5jcdXIxFY+BGcmjCDq45vDJu2Yfs/0jgttSLVGWakkGPnxIllVsE0pe0CgYEAxaRgtIV7TWQO/aiprb8aOakbOtOxKl80WqfsIAiLTZzbuzT+EZSF3vp0l9iSENlhn1stkJea8erkLZEeajqODMh3GUCCtb4yPUwGWqXshsJFGQ/VZRAxVd1cLWU+EoSY1yK7mLCU2DQzpbjz3adTSEjGP+9Wmo+WIdBMy/IP0iECgYAC6pXdooTHrXJm1gt5dlJdJIjuIvmw1VepV+QGaZODvVBuM4HLaZxTMd5Bn11DA9PtKQ0X5Mt+clzSGzosleidG0tPbcaILc2/FS/QFJoXLfPe0ZJROFnI776plu/z2MvFAzpEMln/OEX4XnZQD4ZiVzBNW/0HMh8PVyNd8mm0rQKBgQCvOgztYlm3oUcEleVgR/AEeG744JIyK3zXKKFMNrHSjEwUxdb1NGj3b87P5KDDq787NGIylGIrPz7x5KQtw8KpV7kr3efhb/IY/OhLiI3AD1ZYvCkLevr74VxQTJX5Ajin/E58HEo+5fMa323ASAtS5eWfifAaefO+SyC81LwVUA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgoz3wodw1u5k2XC2tGS0nYrT3Heow4YLn6kMagi+pslA1vOnPjbQuZS6JSxw1VaYUlkkteWSXv12jZ0XIkfMku7xLTVv0COs0UU+Z/+GMs3zkcEJ0YVRIXl5ToMKyXn3iVPZFeePNYUlGa2Im4g+6MC1F/CbRdGL8XkQUjbd+j2gDYzY8NpSxfe5K2ZkDviueJt75yeLYCKkC4FL7wc3d3KtVa4ohvhmQlL9vb5mHsJLoxqnpZ03+WMz63WjlH5vWxovBqhnfODGk2CliSzKFC4MlBW1vYZ/4qbnEf3e098joD59Whjk8QEcmftz0aHfWFtQIPqbhk/vE6ZToGLu7wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://xgzit.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.xgzmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        // 订单超时时间
        String timeout_express = vo.getTimeout_express();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
    /** 主动收单 **/
    public String tradeClose(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);

        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = new String(request.getParameter("WIDTCout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDTCtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"}");

        //请求
        return alipayClient.execute(alipayRequest).getBody();
    }
}
